package com.cqut.hfh.ordering.controller;

import com.alibaba.fastjson.JSON;
import com.cqut.hfh.ordering.pojo.EhyModule;
import com.cqut.hfh.ordering.pojo.EhyRole;
import com.cqut.hfh.ordering.pojo.EhyRoleModule;
import com.cqut.hfh.ordering.service.IModuleService;
import com.cqut.hfh.ordering.service.IRoleService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.hfh.ordering.utils.Message;
import org.aspectj.bridge.MessageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LJH
 * @date 2019/6/23-20:34
 */
@Controller
@RequestMapping("/role")

public class RoleController {

    @Resource
    private IRoleService roleService;
    @Resource
    private IModuleService moduleService;

    /**
     * 添加角色并设置角色对应的模块
     * @param role
     * @param module_ids
     * @return
     */
    @RequestMapping(path = "/saveOrUpdate", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String saveOrUpdate(EhyRole role, @RequestParam("str") String module_ids){

        role.setOptime(new Date());

        if( role.getRole_id() == null){
            role.setRole_id(EntityIdutils.getId());
            int temp = roleService.saveRole(role, module_ids);
            return Message.getReturnMsg(0,"");
        }else{
            int temp = roleService.updateRole(role, module_ids);
        }
        return "";
    }

    /**
     * 通过ID删除角色，以及角色-模块中间表，角色-用户中间表
     * @param role_id
     * @return
     */
    @RequestMapping(path = "/delete", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String deleteById(@RequestParam("roleId") String role_id){

        int temp = roleService.delById(role_id);
        if( temp == 3){
            return "{\"msg\":\"ok\"}";
//            return Message.getReturnMsg(0,"ok");
        }
        return "{\"msg\":\"fail\"}";
//        return MessageUtil.getReturnMsg(0,"fail",null);
    }

    /**
     * 获取所有角色
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping(path = "/findAll", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findRoles(@RequestParam("limit") String limit,
                          @RequestParam("page") String page){

        List<Map<String, Object>> role_list = roleService.getRoles(limit, page);

        for( Map<String, Object> map : role_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }

        Integer count = roleService.getCount();

        return Message.getReturnMsg(0,"", count, role_list);
    }


    /**
     * 获取所有模块
     * @return
     */
    @RequestMapping(path = "/findModel", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findModel(){

        List<EhyModule> result_map = moduleService.getModules();
//        return JSON.toJSONString(result_map.toArray());
        return Message.getReturnMsg(0,"",result_map);

    }

    /**
     * 通过角色ID获取角色信息
     * @param role_id
     * @return
     */
    @PostMapping("/findId")
    @ResponseBody
    public EhyRole findById(@RequestParam("roleId") String role_id){

        Map<String, Object> result_map = roleService.findById(role_id);
        EhyRole e = new EhyRole();
        e.setProperties(result_map);

        return e;
    }


    /**
     * 通过角色ID获取模块信息
     * @return
     */
    @PostMapping(path = "/findRoleModule")
    @ResponseBody
    public List<EhyModule> findRoleModule(@RequestParam("roleId")String role_id){

        List<EhyModule> result_map = moduleService.getModulesById(role_id);

//        return JSON.toJSONString(result_map);

//        return Message.getReturnMsg(0,"",result_map);
        return result_map;
    }


    /**
     * 根据名字查角色
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping(path = "/findRolesByName", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findRolesByName(@RequestParam("limit") String limit,
                                  @RequestParam("page") String page,
                                  @RequestParam("role_name") String role_name){

        List<Map<String, Object>> role_list = roleService.getRolesByName(limit, page, role_name);

        for( Map<String, Object> map : role_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }

        Integer count = roleService.getCount();

        return Message.getReturnMsg(0,"", count, role_list);
    }


    /**
     * 显示所有的角色
     * @return
     */
    @RequestMapping(path = "/findRole",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findAlls(){
        List<EhyRole> result_map = roleService.getRole();
        return Message.getReturnMsg(0,"",result_map);
    }

}
